﻿using Microsoft.Practices.Unity;
using SampleServiceInterfacesAndImplementation;
using Microsoft.Practices.ServiceLocation;
using Microsoft.Practices.Unity.ServiceLocatorAdapter;

namespace UnitySamples
{
    public class Unity_With_Service_Locator__Container_Configured_Registration_of_Types
    {
        public static void Sample_Using_Unity_Only()
        {
            IUnityContainer myContainer = new UnityContainer();
            //In a nutshell this says that every time Unity is asked to provide 
            //an object of type ILogger, it should instantiate a ConsoleLogger object and 
            //return that. 
            myContainer.RegisterType<ILogger, ConsoleLogger>();
            
            //Pretty simple, but it does kind of tightly couples me to Unity. 
            var myServiceInstance = myContainer.Resolve<ILogger>();
            
            myServiceInstance.Write("hi ");
        }

        public static void Sample_Using_Unity_And_Service_Locator()
        {
            IUnityContainer myContainer = new UnityContainer();
            myContainer.RegisterType<ILogger, ConsoleLogger>();

            //wrapping the unity container with the service locator
            ServiceLocator.SetLocatorProvider(() => new UnityServiceLocator(myContainer));

            //using service locator
            var myServiceInstance = ServiceLocator.Current.GetInstance<ILogger>();
            myServiceInstance.Write("hi ");
        }
    }
}